package com.rainner.admin.action;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.admin.action.base.EasyuiAction;
import com.rainner.admin.defined.PermissionDefined;
import com.rainner.admin.defined.SystemDefine;
import com.rainner.admin.javabean.PermissionBean;
import com.rainner.admin.javabean.RolePermissionBean;
import com.rainner.admin.server.PermissionServer;
import com.rainner.admin.server.RolePermissionServer;

@Results({ @Result(name = "rolepermission", type = "freemarker", location = "/WEB-INF/content/manage/rolepermission.ftl") })
public class RolePermissionAction extends EasyuiAction {
	private static final long serialVersionUID = 1L;

	private PermissionServer permissionServer = new PermissionServer();
	private RolePermissionServer rolePermissionServer = new RolePermissionServer();
	private PermissionBean bean = new PermissionBean();
	private String type = null;
	private String roleId;
	private String id;// bean接收得到permissionJudge就是接受不到permissionId,我疯了

	@Actions({ @Action("/rolepermission"), @Action("/role/rolepermission/{type}") })
	public String rolePermission() throws SQLException, Exception {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if (type == null && username != null) {
			return "rolepermission";
		}else if(username ==null){
			return null;
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		roleId = new String(request.getParameter("roleId").getBytes("iso-8859-1"), "utf-8");
		if (SystemDefine.ROLE_PERMISSION_INIT.equals(type) && rolePermissionServer.checkPermission(PermissionDefined.SEARCH_PERMISSION, username.toString())) {
			map.put("total", permissionServer.countPermissionData());
			List<PermissionBean> permission = permissionServer.allPermissionData(rows, page);
			List<RolePermissionBean> rolePermission = rolePermissionServer.allRolePermissionData(roleId);
			for (PermissionBean temp : permission) {
				for (RolePermissionBean rolePermissionBean : rolePermission) {
					if (temp.getPermissionId().equals(rolePermissionBean.getPermissionId())) {
						temp.setPermissionJudge(true);
					}
				}
			}
			map.put("rows", permission);
		} else if (SystemDefine.ROLE_PERMISSION_UPDATE.equals(type) && rolePermissionServer.checkPermission(PermissionDefined.UPDATE_PERMISSION, username.toString())) {
			if (bean.isPermissionJudge()) {
				this.result.setState(rolePermissionServer.addRolePermission(roleId, id));
			} else {
				this.result.setState(rolePermissionServer.delRolePermission(roleId, id));
			}
			map.put("message", this.result);
		}
		return MAP;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setBean(PermissionBean bean) {
		this.bean = bean;
	}
	

}
